package com.vessel.decoration.mvp.ui.fragment.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.vessel.decoration.BuildConfig;
import com.vessel.decoration.R;
import com.vessel.decoration.app.base.MySupportFragment;
import com.vessel.decoration.app.constant.Constants;
import com.vessel.decoration.app.data.entity.ShopInfoResponse;
import com.vessel.decoration.app.utils.RecycleViewDivider;
import com.vessel.decoration.di.component.mine.manage.DaggerBusinessComponent;
import com.vessel.decoration.di.module.mine.manage.BusinessModule;
import com.vessel.decoration.mvp.contract.mine.manage.BusinessContract;
import com.vessel.decoration.mvp.presenter.mine.manage.BusinessPresenter;
import com.vessel.decoration.mvp.ui.adapter.ProductManagerAdapter;
import com.vessel.decoration.mvp.ui.widget.RoundImage;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author vesselzhang
 * @date 2017/12/6
 */

public class MeBusinessFragment extends MySupportFragment<BusinessPresenter> implements BusinessContract.View {

    @BindView(R.id.tv_title)
    TextView mTitleTV;
    @BindView(R.id.tv_right)
    TextView mTitleRightTV;
    @BindView(R.id.business_info_pic)
    RoundImage mPicRI;
    @BindView(R.id.business_info_name)
    TextView mNameTV;
    @BindView(R.id.business_info_type)
    TextView mTypeTV;
    @BindView(R.id.business_info_address)
    TextView mAddressTV;
    @BindView(R.id.business_info_phone)
    TextView mPhoneTV;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @Inject
    ProductManagerAdapter mAdapter;
    ShopInfoResponse shopInfo;
    private AppComponent mAppComponent;

    public static MeBusinessFragment newInstance() {
        return new MeBusinessFragment();
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerBusinessComponent
                .builder()
                .appComponent(appComponent)
                .businessModule(new BusinessModule(this))
                .build()
                .inject(this);
        this.mAppComponent = appComponent;
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_me_business, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mTitleTV.setText(ArmsUtils.getString(getActivity(), R.string.me_item_business));
        mTitleRightTV.setVisibility(View.VISIBLE);
        mTitleRightTV.setText("添加商品");

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(getActivity(), LinearLayoutManager.VERTICAL
                , 20, getResources().getColor(R.color.def_bg)));
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getBusinessInfo();
    }

    @Override
    public void setData(Object data) {

    }

    @OnClick({R.id.iv_left, R.id.tv_right, R.id.business_info_layout})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_left:
                killMyself();
            case R.id.tv_right:
                ARouter.getInstance().build("/app/business/product")
                        .withSerializable(Constants.TYPE, Constants.TYPE_ADD)
                        .navigation();
                break;
            case R.id.business_info_layout:
                if (shopInfo == null) return;
                ARouter.getInstance().build("/app/business/info")
                        .withSerializable("data", shopInfo)
                        .navigation();
                break;
        }
    }

    @Override
    public void setShopInfo(ShopInfoResponse info) {
        this.shopInfo = info;
        if (shopInfo != null) {
            if (!TextUtils.isEmpty(info.getShopPic())) {
                mAppComponent.imageLoader()
                        .loadImage(getActivity(), ImageConfigImpl
                                .builder()
                                .imageView(mPicRI)
                                .url(BuildConfig.APP_DOMAIN + info.getShopPic())
                                .build());
            }
            mNameTV.setText(info.getShopName());
            mAddressTV.setText("地址：" + info.getAddress());
            mPhoneTV.setText("电话：" + info.getPhone());
        }
    }

    @Override
    public void showType(String type) {
        mTypeTV.setText(type);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        ArmsUtils.makeText(getActivity(), message);
    }

    @Override
    public void launchActivity(Intent intent) {

    }

    @Override
    public void killMyself() {
        getActivity().finish();
    }
}
